package com.suxin.websocket.session;

import lombok.extern.slf4j.Slf4j;

import javax.websocket.Session;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Tang
 * @classname SessionHolder
 * @description [  ]
 * @date 2022/3/24 10:22
 */
@Slf4j
public class SimpleSessionHolder implements SessionHolder {

    private static ConcurrentHashMap<String, Session> sessions = new ConcurrentHashMap<>(8);

    @Override
    public void addSession(Session session, String key) {
        sessions.put(key, session);
    }

    @Override
    public void removeSession(String key) {
        try {
            sessions.remove(key).close();
        } catch (IOException e) {
            log.error("客户端移除异常:{}",e.getMessage());
        }
    }

    @Override
    public Session getSession(String key) {
        return sessions.get(key);
    }
}
